package net.kansun.main;

import net.kansun.Parser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Scanner;

/**
 * The portal class where the application startss
 */
public class Main {

   private final static Parser parser;

   static {

      ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
      parser = ctx.getBean("parserController", Parser.class);
   }

   public static void main(String[] args) {

      final Scanner in = new Scanner(System.in);
      final String exit = "exit";


      do {
         String source = null;
         try {

            System.out.println("Please input any number between 0 and 999999999 or type '" + exit + "' to exit: ");

            source = in.nextLine();

            if (source.equals(exit)) {

               in.close();
               System.out.println("bye!");
               System.exit(0);
            }

            Long number = Long.valueOf(source);
            String result = parser.parse(number);

            System.out.println(String.format("[%s] is converted to [%s].", number, result));

         } catch (Exception e) {

            System.err.println(String.format("Got an error while processing input [%s].", source));
         }
      } while (true);
   }
}
